package bultin;

import interpret.Aid;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.io.File;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;

import menuSystems.MainMenu;
import pseudo_ASH.RightTabPane;
import pseudo_ASH.RightTabPane.Tabs;
import code.ErrorCondition;
import code.ObjectC;
import code.TokenType;
import code.Variable;
import code.Variable.varType;

import com.sydwell.service.ArrayPlus;
import com.sydwell.service.GUI;
import com.sydwell.service.Helper;


class Picture_Bi extends BIClass {
	JComponent jo = null;
	ImageIcon imageIcon;

	public Picture_Bi() {
		super("Picture");
		addMethod(new Move());
		addMethod(new GetHeight());
		addMethod(new GetWidth());
		addMethod(new Scale());
		addMethod(new SetHeight());
		addMethod(new SetWidth());
		
		//addMethod(JavaObjectMethods.setBackground);
		//addMethod(JavaObjectMethods.setForeground);
		//addMethod(JavaObjectMethods.getBackground);
		//addMethod(JavaObjectMethods.getForeground);
	}

	@Override
	public JComponent makeJavaComp(String theInstanceName,
			ArrayPlus<TokenType> arguments) throws ErrorCondition {
		String strFile = arguments.get(0).getTokenText();
//		if (icon) {
//			Helper.debug("strFile" + strFile);
//		}
		File picFile = new File(MainMenu.getStartupDirectory()+ "\\" +strFile);
		if (picFile.canRead()) {
			//Helper.debug(" Yes ");
		} else {
			Helper.debug(" No "+ picFile.getAbsolutePath());
			throw new ErrorCondition ("File "+GUI.H_ERROR+ strFile +GUI.END_H_ERROR+ " not found in directory " 
					+ GUI.H_ERROR+ MainMenu.getStartupDirectory() + GUI.END_H_ERROR, Aid.OBJECTS);
		}
		imageIcon = new ImageIcon(picFile.getAbsolutePath());
		jo = new JButton(imageIcon);
		//jo.setSize(new Dimension(500,500));
		//jo.setPreferredSize(new Dimension(500,500));
		//jo.setMinimumSize(new Dimension(500,500));
		//jo.setMaximumSize(new Dimension(500,500));
		jo.setBackground(Color.white);
		RightTabPane.add2OutputDisplay(jo);
		RightTabPane.tab2Display(Tabs.OUTPUT);
		return jo;
	}
	
	@Override
	public String getJavaClassName() {
		return "JButton";
	}
	
	class Scale extends BuiltinMethod {
		public Scale() {
			super("scale");
			super.addParameter(varType.REAL);
			//super.setReturnType(varType.INTEGER);
		}

		@Override
		public Variable excuteMethod(ObjectC nop, ArrayPlus<TokenType> arrayPlus) {
			//Helper.debug("Still to do "+ arrayPlus.get(0).hasLiteralValue(), true);
			double scale = arrayPlus.get(0).getTokenVariable().getRealValue();
			int h = (int)(jo.getMaximumSize().height * scale);
			int w = (int)(jo.getMaximumSize().width * scale);
			jo.setMaximumSize(new Dimension(w, h));
			jo.setMinimumSize(new Dimension(w, h));
			jo.setPreferredSize(new Dimension(w, h));
			jo.setSize(new Dimension(w, h));
			Image newimg = imageIcon.getImage().getScaledInstance(w, h,  java.awt.Image.SCALE_SMOOTH);
			((JButton)jo).setIcon(new ImageIcon(newimg));
			return null;
		}
		
		@Override
		public String convert2Java(ArrayPlus<TokenType> arguments, TokenType target) {
			//time = (int) (Math.random() * 20);
		//	Helper.debug("target " + target, true);
			String out = arguments.get(0) + " = (int) (Math.random() * " + target + ")";

			out = out.replace(";", "");
			out = out.replace("\r", "");
			out = out.replace("\n", "");
			return out;
		}

		@Override
		public String information() {
			String out = "\nWill scale the current image by the factor supplied";
			out += "\nUsage:" ;
			out += "\n<picture>.scale(<factor>)"; 
			out += "\nWhere: <picture> is the picture to scale";
			out += "\nWhere: <factor> is the scaling factor ";
			out += "\n a factor of 0.5 will reduce the image by half";
			return out;
		}
	}
	
	class SetHeight extends BuiltinMethod {
		public SetHeight() {
			super("setHeight");
			super.addParameter(varType.INTEGER);
		//	super.setReturnType(varType.INTEGER);
		}

		@Override
		public Variable excuteMethod(ObjectC nop, ArrayPlus<TokenType> arrayPlus) {
			//Helper.debug("Still to do "+ arrayPlus.get(0).hasLiteralValue(), true);
			int h = arrayPlus.get(0).getTokenVariable().getIntValue();
			int w = jo.getMaximumSize().width;
			
			jo.setMaximumSize(new Dimension(w, h));
			jo.setMinimumSize(new Dimension(w, h));
			jo.setPreferredSize(new Dimension(w, h));
			jo.setSize(new Dimension(w, h));
			Image newimg = imageIcon.getImage().getScaledInstance(w, h,  java.awt.Image.SCALE_SMOOTH);
			((JButton)jo).setIcon(new ImageIcon(newimg));
			//Helper.debug("target " + h + jo.getMaximumSize());
			return null;
		}
		
		@Override
		public String convert2Java(ArrayPlus<TokenType> arguments, TokenType target) {
			//time = (int) (Math.random() * 20);
		//	Helper.debug("target " + target, true);
			String out = arguments.get(0) + " = (int) (Math.random() * " + target + ")";

			out = out.replace(";", "");
			out = out.replace("\r", "");
			out = out.replace("\n", "");
			return out;
		}

		@Override
		public String information() {
			String out = "\nWill set the height of the current image ";
			out += "\nUsage:" ;
			out += "\n<picture>.setHeight(<factor>)"; 
			out += "\nWhere: <picture> is the picture to scale";
			out += "\nWhere: <height> is the new height of the image";
			return out;
		}
	}
	class SetWidth extends BuiltinMethod {
		public SetWidth() {
			super("setWidth");
			super.addParameter(varType.INTEGER);
			//super.setReturnType(varType.INTEGER);
		}

		@Override
		public Variable excuteMethod(ObjectC nop, ArrayPlus<TokenType> arrayPlus) {
			//Helper.debug("Still to do "+ arrayPlus.get(0).hasLiteralValue(), true);
			int w = arrayPlus.get(0).getTokenVariable().getIntValue();
			int h = jo.getMaximumSize().height;
			jo.setMaximumSize(new Dimension(w, h));
			jo.setMinimumSize(new Dimension(w, h));
			jo.setPreferredSize(new Dimension(w, h));
			jo.setSize(new Dimension(w, h));
			Image newimg = imageIcon.getImage().getScaledInstance(w, h,  java.awt.Image.SCALE_SMOOTH);
			((JButton)jo).setIcon(new ImageIcon(newimg));
			return null;
		}
		
		@Override
		public String convert2Java(ArrayPlus<TokenType> arguments, TokenType target) {
			//time = (int) (Math.random() * 20);
		//	Helper.debug("target " + target, true);
			String out = arguments.get(0) + " = (int) (Math.random() * " + target + ")";

			out = out.replace(";", "");
			out = out.replace("\r", "");
			out = out.replace("\n", "");
			return out;
		}

		@Override
		public String information() {
			String out = "\nWill set the width of the current picture ";
			out += "\nUsage:" ;
			out += "\n<picture>.setHeight(<factor>)"; 
			out += "\nWhere: <picture> is the current picture to scale";
			out += "\nWhere: <height> is the new height of the image";
			return out;
		}
	}
	class GetHeight extends BuiltinMethod {
		public GetHeight() {
			super("getHeight");
			//super.addParameter(varType.INTEGER);
			super.setReturnType(varType.INTEGER);
		}

		@Override
		public Variable excuteMethod(ObjectC nop, ArrayPlus<TokenType> arrayPlus) {
			//Helper.debug("Still to do "+ arrayPlus.get(0).hasLiteralValue(), true);
			returnVariable.setValue(jo.getMaximumSize().height);
			return returnVariable;
		}
		
		@Override
		public String convert2Java(ArrayPlus<TokenType> arguments, TokenType target) {
			//time = (int) (Math.random() * 20);
		//	Helper.debug("target " + target, true);
			String out = arguments.get(0) + " = (int) (Math.random() * " + target + ")";

			out = out.replace(";", "");
			out = out.replace("\r", "");
			out = out.replace("\n", "");
			return out;
		}

		@Override
		public String information() {
			String out = "\nWill get the height of the current image ";
			out += "\nUsage:" ;
			out += "\n<picture>.getHeight()"; 
			out += "\nWhere: <picture> is the picture to get the height of";
			return out;
		}
	}
	class GetWidth extends BuiltinMethod {
		public GetWidth() {
			super("getWidth");
			//super.addParameter(varType.INTEGER);
			super.setReturnType(varType.INTEGER);
		}

		@Override
		public Variable excuteMethod(ObjectC nop, ArrayPlus<TokenType> arrayPlus) {
			returnVariable.setValue(jo.getMaximumSize().width);
			return returnVariable;
		}
		
		@Override
		public String convert2Java(ArrayPlus<TokenType> arguments, TokenType target) {
			//time = (int) (Math.random() * 20);
		//	Helper.debug("target " + target, true);
			String out = arguments.get(0) + " = (int) (Math.random() * " + target + ")";

			out = out.replace(";", "");
			out = out.replace("\r", "");
			out = out.replace("\n", "");
			return out;
		}

		@Override
		public String information() {
			String out = "\nWill get the width of the current image ";
			out += "\nUsage:" ;
			out += "\n<picture>.getWidth()"; 
			out += "\nWhere: <picture> is the picture to get the width of";
			return out;
		}
	}
	
	class Move extends BuiltinMethod {
		public Move() {
			super("move");
			super.addParameter(varType.INTEGER);
			super.setReturnType(varType.INTEGER);
		}

		@Override
		public Variable excuteMethod(ObjectC nop, ArrayPlus<TokenType> arrayPlus) {
			//Helper.debug("Still to do "+ arrayPlus.get(0).hasLiteralValue(), true);
			int range = arrayPlus.get(0).getTokenVariable().getIntValue();
			range = (int)(Math.random() * range);
			returnVariable.setValue(range);
			return returnVariable;
		}
		
		@Override
		public String convert2Java(ArrayPlus<TokenType> arguments, TokenType target) {
			//time = (int) (Math.random() * 20);
		//	Helper.debug("target " + target, true);
			String out = arguments.get(0) + " = (int) (Math.random() * " + target + ")";

			out = out.replace(";", "");
			out = out.replace("\r", "");
			out = out.replace("\n", "");
			return out;
		}

		@Override
		public String information() {
			// TODO Auto-generated method stub
			return null;
		}
	}
	
//	class OnClick extends BuiltinMethod {
//		
//		public OnClick() {
//			super("onClick", true, false);
//			super.addParameter(varType.INTEGER);
//		}
//
//		@Override
//		public Variable excuteMethod(ArrayPlus<TokenType> argumentList) {
////			try {
////				ErrorCondition.checkArugmentList(argumentList, argumentOptions);
////			} catch (ErrorCondition e) {
////				return returnVariable;
////			}
//			String str = argumentList.get(0).getTokenVariable().getStrValue();
//			returnVariable.setAndConvert("" + str.length());
//			return returnVariable;
//		}
//	}
}
